package com.letv.android.client.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.letv.core.utils.LogInfo;
import com.letv.core.utils.StatisticsUtils;

/**
 * home键监听
 * 
 * @author jiancong
 *
 */
public class HomeKeyEventReceiver extends BroadcastReceiver {
    private boolean mIsHomeClicked = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        final String SYSTEM_REASON = "reason";
        final String SYSTEM_HOME_KEY = "homekey";
        final String SYSTEM_HOME_KEY_LONG = "recentapps";

        String action = intent.getAction();
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            String reason = intent.getStringExtra(SYSTEM_REASON);
            // 按home键,程序到了后台
            if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
                LogInfo.LogStatistics("home click");
                mIsHomeClicked = true;
                // 统计-退出应用
                StatisticsUtils.statisticsLogin(context, 1);
            } else if (TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)) {
                // 长按home键,显示最近使用的程序列表
                LogInfo.LogStatistics("home long click");
            }
        }
    }

    /**
     * 是否点击了home键
     * 
     * @return
     */
    public boolean isHomeClicked() {
        return mIsHomeClicked;
    }
    
    /**
     * 设置是否点击了home键
     * 
     * @param IsHomeClicked home键点击后再次打开应用会将其重置为false
     */
    public void setIsHomeClicked(boolean IsHomeClicked) {
        this.mIsHomeClicked = IsHomeClicked;
    }
}
